package cn.ike.web.controller.cargo;

import cn.ike.dao.cargo.ExtCproductDao;
import cn.ike.domain.cargo.*;
import cn.ike.domain.system.User;
import cn.ike.service.cargo.ExtCproductService;
import cn.ike.service.cargo.FactoryService;
import cn.ike.web.controller.BaseController;
import cn.ike.web.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@Log4j
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {

    // 附件
    @Reference
    private ExtCproductService extCproductService;
    // 厂家
    @Reference
    private FactoryService factoryService;
    // 文件上传
    @Autowired
    private FileUploadUtil fileUploadUtil;

    /**
     * 1.货物附件的列表分页
     */
    @RequestMapping("/list")
    public ModelAndView list(
            String contractId, String contractProductId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize) {
        ModelAndView model = new ModelAndView();
        try {
            log.info("执行货物附件列表分页查询开始...");
            //1.查询附件的生产厂家
            FactoryExample factoryExample = new FactoryExample();
            factoryExample.createCriteria().andCtypeEqualTo("附件");
            List<Factory> factoryList = factoryService.findAll(factoryExample);

            //2.查询当前货物下的所有附件
            ExtCproductExample extCproductExample = new ExtCproductExample();
            extCproductExample.createCriteria().andContractProductIdEqualTo(contractProductId);
            PageInfo<ExtCproduct> pageInfo = extCproductService.findByPage(extCproductExample, pageNum, pageSize);

            model.addObject("factoryList", factoryList);
            model.addObject("pageInfo", pageInfo);
            model.addObject("contractId", contractId);
            model.addObject("contractProductId", contractProductId);
            model.setViewName("cargo/extc/extc-list");
            log.info("执行货物附件列表分页查询结束...");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("执行货物附件列表分页查询出现异常！", e);
        }
        return model;
    }

    /**
     * 2.附件的添加或修改
     */
    @RequestMapping("/edit")
    public ModelAndView edit(ExtCproduct extCproduct, MultipartFile productPhoto) {
        ModelAndView model = new ModelAndView();
        String contractId = extCproduct.getContractId();
        String contractProductId = extCproduct.getContractProductId();
        try {
            String extCproductId = extCproduct.getId();
            if (StringUtils.isEmpty(extCproductId)) {
                log.info("执行附件的添加或修改开始...");
                extCproduct.setCompanyId(getLoginCompanyId());
                extCproduct.setCompanyName(getLoginCompanyName());
                /*处理文件上传*/
               if (productPhoto != null && productPhoto.getSize() > 0){
                    // 处理文件上传
                    String fileUrl = "http://" + fileUploadUtil.upload(productPhoto);
                    extCproduct.setProductImage(fileUrl);
                }
                /*记录创建人、创建人的部门(后期列表数据权限控制使用)*/
                extCproduct.setCreateBy(getLoginUser().getId());
                extCproduct.setCreateDept(getLoginUser().getDeptId());
                extCproductService.save(extCproduct);
                log.info("执行附件的添加或修改结束...");
            } else {
                log.info("执行附件的添加或修改开始...");
                extCproductService.update(extCproduct);
                log.info("执行附件的添加或修改结束...");
            }
            model.setViewName("redirect:/cargo/extCproduct/list?contractId=" + contractId + "&contractProductId=" +contractProductId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("执行附件的添加或修改出现异常！", e);
        }
        return model;
    }

    /**
     * 3. 进入附件的修改页面
     */
    @RequestMapping("/toUpdate")
    public ModelAndView toUpdate(String contractId, String contractProductId, String id) {
        ModelAndView model = new ModelAndView();
        try {
            log.info("执行进入附件的修改页面开始...");
            // 查询附件信息
            ExtCproduct extCproduct = extCproductService.findById(id);

            // 查询所有生产附件的厂家
            FactoryExample factoryExample = new FactoryExample();
            factoryExample.createCriteria().andCtypeEqualTo("附件");
            List<Factory> factoryList = factoryService.findAll(factoryExample);

            model.addObject("extCproduct", extCproduct);
            model.addObject("factoryList", factoryList);
            model.addObject("contractId",contractId);
            model.addObject("contractProductId", contractProductId);
            model.setViewName("cargo/extc/extc-update");
            log.info("执行进入附件的修改页面结束...");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("执行进入附件的修改页面出现异常！", e);
        }
        return model;
    }

    /**
     * 删除附件
     */
    @RequestMapping("delete")
    public ModelAndView delete(String contractId, String contractProductId, String id) {
        ModelAndView model = new ModelAndView();
        try {
            log.info("执行删除附件开始...");
            extCproductService.delete(id);
            model.setViewName("redirect:/cargo/extCproduct/list?contractId=" + contractId + "&contractProductId=" +contractProductId);
            log.info("执行删除附件结束...");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("执行删除附件出现异常！", e);
        }
        return model;
    }
}

